package cn.com.yiqi.wsc.portal.exception;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultEnum;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 何刚
 * @ClassName: ${class}
 * @Description: TODO
 * @date 2016/11/30
 */
@ControllerAdvice
public class ApplicationControllerExceptionHandler {

    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result handlerUnauthorizedException(HttpServletRequest req, Exception e) {

        Result result = new Result();

        result.setStatus(ResultEnum.OTHER.getValue());
        result.setErrMsg("无权限");

        return result;
    }


    @ExceptionHandler(value = Throwable.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result handlerError(HttpServletRequest req, Exception e) {

        e.printStackTrace();

        Result result = new Result();

        result.setStatus(ResultEnum.OTHER.getValue());
        result.setErrMsg("服务器内部错误，请稍后再试");

        return result;
    }
}